package com.kevin.chat;

import java.util.HashSet;
import java.util.Set;

import org.directwebremoting.Browser;

public class UserManager {
	/** 保存当前在线用户列表 */
	private static Set<User> users = new HashSet<User>();
	public static boolean addUser(String id,String username){
		boolean flag = false;
		User user  = new User(id, username);
		users.add(user);
		flag = true;
		return flag;
	}
	public static Set<User> getUser(){
		return users;
	}
	public static boolean removeUser(User user){
		boolean flag = false;
		users.remove(user);
		for(User usr:users){
		}
		flag = true;
		//通知所有在线用户 改用户下线
		Browser.withAllSessions(new Runnable() {			
			public void run() {
				// TODO Auto-generated method stub
				org.directwebremoting.ui.dwr.Util.removeAllOptions("users");
				org.directwebremoting.ui.dwr.Util.addOptions("users", UserManager.getUser(), "username");
				org.directwebremoting.ui.dwr.Util.removeAllOptions("receiver");
				org.directwebremoting.ui.dwr.Util.addOptions("receiver", UserManager.getUser(),"userid","username");
				
			}
		});
		return flag;
	}	
}
